//
//  GQMusicLibraryViewController.m
//  MyMusic
//
//  Created by sunny on 16/4/2.
//  Copyright © 2016年 sunny. All rights reserved.
//

#import "GQMusicLibraryViewController.h"
#import "GQMVViewController.h"
#import "GQRankVewController.h"
#import "GQRecommandViewController.h"
#import "GQSongSheetController.h"
#import "TingMusic-Swift.h"

#define topViewH 30
@interface GQMusicLibraryViewController () <UIScrollViewDelegate>
@property (nonatomic,strong)UIButton *lastSelectBtn;

@property (nonatomic,weak)UIView *topView;

@property (nonatomic,weak)UIView *underline;

@property (nonatomic,weak)UIScrollView *scrollView;

@end

@implementation GQMusicLibraryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
 
//    设置导航条上的内容
    [self setupNavBar];
    
//    添加所有的子控制器
    [self setupAllChildViewControllers];
    
//    添加上面的按钮view
    [self addTopView];

//    添加下面的scrollView
    [self setupScrollView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
//    默认点击了第一个按钮
    [self topBtnClick:self.topView.subviews[0]];
    
//    处理通知
    [self dealNotification];
}

#pragma mark - 处理通知
- (void)dealNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationMethod1) name:GQSongSheetMoreButtonDidClickNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationMethod2) name:GQMVMoreButtonDidClickNotification object:nil];
}

- (void)notificationMethod1
{
    [self topBtnClick:self.topView.subviews[1]];
}

- (void)notificationMethod2
{
    [self topBtnClick:self.topView.subviews[2]];
}

#pragma mark - 设置导航条上的内容
- (void)setupNavBar
{
//    设置中间的view
    self.navigationItem.title = @"音乐库";
    
//    设置左边的button
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem addButtonWithImage:[UIImage imageNamed:@"cm2_topbar_icn_mic"] highlightedfImage:[UIImage imageNamed:@"cm2_toptab_mike_new_prs"] addTarget:self action:@selector(mike)];
        
//    设置右边的button
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem addButtonWithImage:[UIImage imageNamed:@"cm2_topbar_icn_playing"] highlightedfImage:[UIImage imageNamed:@"cm2_topbar_icn_playing_prs"] addTarget:self action:@selector(playing:)];
    
}

#pragma mark - 点击了话筒
//点击了话筒
- (void)mike
{
    
}

#pragma mark - 播放音乐
//播放按钮的点击
- (void)playing:(UIButton *)btn
{
    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    UIViewController *vc = [[UIStoryboard storyboardWithName:@"QQListTVC" bundle:nil]instantiateInitialViewController];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -添加所有的子控制器
- (void)setupAllChildViewControllers
{
//    个性推荐
    GQRecommandViewController *recommandVc = [[GQRecommandViewController alloc]init];
//    设置标题
    recommandVc.title = @"个性推荐";
    [self addChildViewController:recommandVc];
    
//    歌单
    GQSongSheetController *sheetVc = [GQSongSheetController SongSheetController];
    sheetVc.title = @"歌单";
    [self addChildViewController:sheetVc];
    
//    MV
    GQMVViewController *mainRadioVc = [GQMVViewController mvViewController];
    mainRadioVc.title = @"MV";
    [self addChildViewController:mainRadioVc];
    
//    排行榜
    GQRankVewController *rankVc = [[GQRankVewController alloc] initWithStyle:UITableViewStyleGrouped];
    rankVc.title = @"排行榜";
    [self addChildViewController:rankVc];
    
}

#pragma mark - 添加顶部的view
- (void)addTopView
{
    CGFloat y = self.navigationController.navigationBarHidden ? 20 : 64;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, y, GQUIScreenW, topViewH)];
    [self.view addSubview:topView];
    topView.backgroundColor = [UIColor whiteColor];
    _topView = topView;
    
//    添加按钮
    [self addButtonsToView:topView];
    
//    添加下划线
    [self setupUnderline];
}

#pragma mark - 添加按钮
- (void)addButtonsToView:(UIView *)topView
{
    NSInteger count = self.childViewControllers.count;
    CGFloat x = 0;
    CGFloat w = GQUIScreenW / count;
    CGFloat h = topView.gq_height;
    
    for (NSInteger i = 0; i < count; i++) {
        x = i * w;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, w, h)];
//        给按钮绑定标识
        btn.tag = i;
//        设置按钮的文字
        [btn setTitle:self.childViewControllers[i].title forState:UIControlStateNormal];
//        设置按钮的字体颜色
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
//        给按钮添加点击事件
        [btn addTarget:self action:@selector(topBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:btn];
        
        if (i == 0) {
            [self topBtnClick:btn];
        }
    }
}

#pragma mark - 添加下划线
- (void)setupUnderline
{
    UIButton *btn = self.topView.subviews[0];
    UIView *underline = [[UIView alloc] init];
    CGFloat underlineH = 2;
    CGFloat underlineY = self.topView.gq_height - underlineH;
    underline.frame = CGRectMake(0, underlineY, 80, underlineH);
    underline.gq_centerX = btn.gq_width * 0.5;
    underline.backgroundColor = [btn titleColorForState:UIControlStateSelected];
    self.underline = underline;
    [self.topView addSubview:underline];
}

#pragma mark - 点击了按钮
- (void)topBtnClick:(UIButton *)btn
{
//    选中按钮
    [self selectBtn:btn];
    
//    移动下划线
    [UIView animateWithDuration:0.25 animations:^{
//        计算按钮文字的宽度
        CGFloat titleButtonW = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]}].width;
        self.underline.gq_width = titleButtonW + 10;
        self.underline.gq_centerX = btn.gq_centerX;
        
//        设置滚动位置
        self.scrollView.contentOffset = CGPointMake(btn.tag * GQUIScreenW, 0);
    }];;
    
//    将对应的子控制器的view添加到scrollView
    UIViewController *vc = self.childViewControllers[btn.tag];
    UIView *view = vc.view;
    view.frame = CGRectMake(btn.tag * GQUIScreenW, 0, self.scrollView.gq_width, self.scrollView.gq_height);
    [self.scrollView addSubview:view];
    
}

#pragma mark - 选中按钮
- (void)selectBtn:(UIButton *)btn
{
    self.lastSelectBtn.selected = NO;
    btn.selected = YES;
    self.lastSelectBtn = btn;
}

#pragma mark - 添加下面的scrollView
- (void)setupScrollView
{
    CGFloat y = self.navigationController.navigationBarHidden ? 20 : 64;
    y += topViewH;
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = CGRectMake(0, y , GQUIScreenW, GQUIScreenH - y);
    _scrollView = scrollView;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    scrollView.pagingEnabled = YES;
    //    设置scrollView的contentSize
    self.scrollView.contentSize = CGSizeMake(self.childViewControllers.count * GQUIScreenW, 0);
}

#pragma ark - scrollView的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //    获取对应下标的按钮
        NSInteger index = scrollView.contentOffset.x / GQUIScreenW;
        UIButton *button = self.topView.subviews[index];
    
    //    点击按钮
        [self topBtnClick:button];
}
@end
